Fix data loss in unbounded_queue under concurrent single-element operations#3
Draft
Fix data loss in unbounded_queue under concurrent single-element operations#3
Conversation
Co-authored-by: ifplusor <9999114+ifplusor@users.noreply.github.com>
Replace nullptr with lock_sentinel to prevent race conditions during single-element pop operations. This fix is based on the Go implementation which resolved the same issue. The bug occurred when multiple threads competed to pop the last element - the original code temporarily set head_ to nullptr, causing race conditions and potential data loss. The fix uses a special lock_sentinel value that other threads recognize and wait for, ensuring proper synchronization. Co-authored-by: ifplusor <9999114+ifplusor@users.noreply.github.com>
Co-authored-by: ifplusor <9999114+ifplusor@users.noreply.github.com>
…iciency - Consolidated wait_unlock() and wait_stable() into a single wait_for_unlock_or_stable() helper - Replaced inefficient busy-wait loop with CPU pause instructions for x86/ARM - Improved comments to clarify active vs passive spinning behavior - All tests still pass Co-authored-by: ifplusor <9999114+ifplusor@users.noreply.github.com>
Co-authored-by: ifplusor <9999114+ifplusor@users.noreply.github.com>
Co-authored-by: ifplusor <9999114+ifplusor@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix data loss issue in unbounded_queue under high concurrency
Fix data loss in unbounded_queue under concurrent single-element operations
Nov 23, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
pop_impl()method temporarily sethead_tonullptrwhen processing the last element, creating a race window where concurrent poppers could observe inconsistent queue state, leading to lost or duplicate elements.Changes
lock_sentinelas a distinct locked state instead of usingnullptrlock_sentinelwhen processing single-element case, ensuring other threads wait rather than racewait_for_unlock_or_stable()handles both lock waiting and push-pop conflicts with CPU pause instructions (x86/ARM)Before/After
Fix follows the approach from the Go port which resolved this same issue.
Testing
Added
ConcurrentSingleElementStresstest: 10K iterations with 4 competing consumers specifically targeting the single-element race condition. Verifies no data loss or duplication.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
esm.ubuntu.com/usr/lib/apt/methods/https /usr/lib/apt/methods/https(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.